//
//  HYSearchViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/14.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYTypeViewController.h"
#import "HYGameInfoViewController.h"
#import "HYTypeCell.h"
#import "HYOnlineCell.h"

#import "HYSearchModel.h"
@interface HYTypeViewController ()<UITableViewDelegate, UITableViewDataSource,HYTextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat cellH;
@property (nonatomic, strong) NSArray *array;

@property (nonatomic, strong) HYSearchList *list;
@end

@implementation HYTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.search.hydelegate = self;
    self.search.placeholder = @"搜索游戏";
    [CCNotice addObs:self sel:@selector(actionForSearchCellItem:) name:HYTypeCellItemClick obj:nil];
    [CCNotice addObs:self sel:@selector(actionFoneOnlineCellInTypeVC:) name:CCNoticeNameOnlineCellClick obj:nil];
    
    
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForTypeVCRefresh:)];
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.height.mas_equalTo(SCREEN_HEIGHT);
        }];
    }
    return _tableView;
}
- (void)setList:(HYSearchList *)list
{
    if (_list != list)
    {
        _list = list;
        [self.tableView reloadData];
    }
}
- (void)actionForRightNaviBar
{
    if ([self.search.text length] < 1)
        return [CCAlert showMessage:@"输入内容" hidden:nil];
    
    self.text = self.search.text;
    self.name = @"";
    self.prop = @"";
    [self request];
}

- (void)actionForSearchCellItem:(NSNotification *)notify
{
    self.name = [notify.userInfo valueForKey:@"name"];
    [self request];
}
- (void)actionFoneOnlineCellInTypeVC:(NSNotification *)notify
{
    HYGameInfoViewController *infoVC = [[HYGameInfoViewController alloc]init];
    infoVC.gameid = [notify.userInfo valueForKey:@"gameid"];
    [self.navigationController pushViewController:infoVC animated:YES];
}
- (void)actionForTypeVCRefresh:(UITableView *)view
{
    [self request];
}
#pragma mark - UITableViewDataSource 事件
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([self.list.games count] > 0 && [self.list.grids count] > 0)  return 2;
    else if ([self.list.games count] > 0 || [self.list.games count] > 0) return 1;
    else return 0;
}
#pragma mark - HYTextFieldDelegate 事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self actionForRightNaviBar];
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (section == 0)?1:self.list.games.count;
}

static NSString *identifier;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && [self.list.games count] > 0 && [self.list.grids count] > 0)
    {
        identifier = [NSString stringWithFormat:@"searchCell%ld",(long)indexPath.section];
        HYTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[HYTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.grids = self.list.grids;
        self.cellH = CGRectGetHeight(cell.bounds);
        return cell;
    }
    else
    {
        identifier = @"HYOnlineCell";
        HYOnlineCell *cell= [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[HYOnlineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.model = self.list.games[indexPath.row];
        return cell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate 事件
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && [self.list.games count] > 0 && [self.list.grids count] > 0)  return self.cellH;
    else return HYOnlineCellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)request
{
    [self startHUD];
    NSDictionary *para = @{@"property":self.prop?self.prop:@"",
                           @"name":self.name?self.name:@"",
                           @"text":self.text?self.text:@"",
                           @"pageNumb":self.pageNumb?self.pageNumb:@""};
    
    [CCHttp post:API_SEARCH_LIST para:para success:^(id  _Nonnull response) {
        [self endHUD];
        self.list = [HYSearchList mj_objectWithKeyValues:response];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
        [self.tableView.mj_header endRefreshing];
    }];
    
//    [HYHttps post:API_SEARCH_LIST para:para complete:^(id response) {
//        [self endHUD];
//        self.list = [HYSearchList mj_objectWithKeyValues:response];
//        [self.tableView.mj_header endRefreshing];
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//        [self.tableView.mj_header endRefreshing];
//    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.search.bounds = CGRectMake(0, 0, SCREEN_MIN_LENGTH - LeftToRightGap, TitleViewHeight);
    [self request];
}
- (void)dealloc
{
    [CCNotice rmObs:self name:HYTypeCellItemClick obj:nil];
    [CCNotice rmObs:self name:CCNoticeNameOnlineCellClick obj:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
